<?php

namespace common\jobs\commission;

use Yii;
use common\jobs\BaseJob;
use yii\console\Exception;
use common\models\user\UserBranch;
use common\models\member\MemberAccount;
use common\models\member\Member;

/**
 * 订单返佣任务处理
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class OpenAccountJob2 extends BaseJob {

    public $member_id = null;

    /**
     * @inheritdoc
     */
    public function execute($queue) {
        if ($this->member_id == null) {
            Yii::error('member_id is null!', __METHOD__);
            return false;
        }
        //获取会员信息
        $member = Member::findOne($this->member_id);
        Yii::error([
            $this->member_id,
            !$member || $member->account,
        ]);
        if (!$member) {
            return $this->dumpError('【' . $this->member_id . '】会员信息不存在！');
        }
        if ($member->account) {
            return $this->dumpError('【' . $this->member_id . '】已开户！');
        } else {
            Yii::error([
                '未开户~~~~~~~~~~~~~',
                $member->attributes,
            ]);
        }
        $account = new MemberAccount();
        $account->attributes = [
            'member_id' => $member->id,
            'user_id' => $member->user_id,
            'status' => MemberAccount::STATUS_ACTIVE,
            'deleted_at' => 0,
        ];
        Yii::error([
            '账户信息~~~~~~~~~~~~~',
            $account->attributes,
        ]);
        $oldBranch = UserBranch::find()->active()->andWhere([
            'user_id' => $member->user_id,
        ])->one();

        Yii::error([
            '分支信息~~~~~~~~~~~~~',
            $oldBranch ? $oldBranch->attributes : null,
        ]);

        $newBranch = new UserBranch();
        $newBranch->attributes = [
            'user_id' => $member->user_id,
            'member_id' => $member->id,
            'member_user_id' => $member->user_id,
            'status' => UserBranch::STATUS_ACTIVE,
        ];
        Yii::error([
            '新分支信息~~~~~~~~~~~~~',
            $newBranch->attributes,
        ]);
        //开启事务保存
        $transaction = Yii::$app->db->beginTransaction();
        try {
            Yii::error([
                '帐号开始？？？~~~~~~~~~~~~~',
            ]);
            if (!$account->save()) {
                Yii::error([
                    '帐号保存失败~~~~~~~~~~~~~',
                    $account->errors
                ]);
                throw new Exception($account->getErrorMsg);
            }
            Yii::error([
                '帐号~~~~~~~~~~~~~',
                $account->id,
            ]);
            if ($oldBranch) {
                $oldBranch->deleted_at = time();
                if (!$oldBranch->save()) {
                    throw new Exception($oldBranch->getErrorMsg);
                }
            }
            if (!$newBranch->save()) {
                throw new Exception($newBranch->getErrorMsg);
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            Yii::error([
                '出错了~~~~~~~~~~~~~',
                $e->getMessage(),
            ]);
            return $this->dumpError($e->getMessage());
        }
        return $this->dumpSuccess('开户成功');
    }

}
